package com.hmbe.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmbe.dto.Result;
import com.hmbe.dto.UserDTO;
import com.hmbe.entity.Follow;
import com.hmbe.mapper.FollowMapper;
import com.hmbe.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmbe.service.IUserService;
import com.hmbe.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private IUserService userService;

    @Override
    public Result follow(Long followUserId, Boolean isFollow) {
        Long userId = UserHolder.getUser().getId();
        String key = "follows:" + userId;
        //1.查询是否已经关注过
        if (!isFollow) {
            //取消关注
            //1.1.删除redis中的关注关系
            redisTemplate.opsForSet().remove(key, followUserId.toString());
            //1.2.删除mysql中的关注关系
            boolean isRemove = remove(new QueryWrapper<Follow>().eq("follow_user_id", followUserId).eq("user_id", userId));
            return Result.ok(isRemove);
        }
        Follow follow = new Follow();
        follow.setFollowUserId(followUserId);
        follow.setUserId(userId);
        //2.关注
        boolean isSave = save(createFollow(followUserId, userId));
        if (isSave) {
            //2.1.保存redis中的关注关系
            redisTemplate.opsForSet().add(key, followUserId.toString());
        }
        //3.返回结果
        return Result.ok(isSave);
    }

    private Follow createFollow(Long followUserId, Long userId) {
        Follow follow = new Follow();
        follow.setFollowUserId(followUserId);
        follow.setUserId(userId);
        return follow;
    }

    @Override
    public Result isFollow(Long followUserId) {
        Long userId = UserHolder.getUser().getId();
        //1.查询是否已经关注过
        Follow follow = getOne(new QueryWrapper<Follow>().eq("follow_user_id", followUserId).eq("user_id", userId));
        return Result.ok(follow != null);
    }

    @Override
    public Result queryFollowsCommon(Long followUserId) {
        Long userId = UserHolder.getUser().getId();
        //1.从redis中查询共同关注
        String key = "follows:" + userId;
        Set<String> intersect = redisTemplate.opsForSet().intersect(key, "follows:" + followUserId);
        if (intersect == null || intersect.isEmpty()) {
            return Result.ok(Collections.emptyList());
        }
        //2.取出共同关注人id
        List<Long> idList = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        //3.查询共同关注人信息
        List<UserDTO> userDTOS = userService.listByIds(idList)
                .stream().map(user ->
                        BeanUtil.copyProperties(user, UserDTO.class)
                ).collect(Collectors.toList());
        return Result.ok(userDTOS);
    }
}
